package com.sensetime.jv.entity;

import com.sensetime.jv.enums.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;

import static com.sensetime.jv.constants.CustomConstant.REQUEST_ID;

@Slf4j
public class ReturnWrapper<T> {

    private int code;
    private String msg;
    private T data;
    private String requestId;

    /* ---------- 静态快速构造 ---------- */
    public static <T> ReturnWrapper<T> success(T data) {
        return new ReturnWrapper<>(StatusEnum.SUCCESS, data);
    }

    public static <T> ReturnWrapper<T> success() {
        return success(null);
    }

    public static <T> ReturnWrapper<T> error(StatusEnum status) {
        return new ReturnWrapper<>(status, null);
    }

    /* ---------- 构造方法 ---------- */
    public ReturnWrapper() {}

    public ReturnWrapper(StatusEnum status, T data) {
        this.code = status.getCode();
        this.msg = status.getDesc();
        this.data = data;
        this.requestId = String.valueOf(ThreadContext.get(REQUEST_ID));
    }

    /* ---------- Getter/Setter ---------- */
    public int getCode() {
        return code;
    }

    public ReturnWrapper<T> code(int code) {
        this.code = code;
        this.requestId = (String) ThreadContext.get(REQUEST_ID);
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ReturnWrapper<T> msg(String msg) {
        this.msg = msg;
        this.requestId = (String) ThreadContext.get(REQUEST_ID);
        return this;
    }

    public T getData() {
        return data;
    }

    public ReturnWrapper<T> data(T data) {
        this.data = data;
        this.requestId = (String) ThreadContext.get(REQUEST_ID);
        return this;
    }

    public String getRequestId() {
        return requestId;
    }
}